package cn.kingyea.bigdata.hbase;

import java.io.File;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.security.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import cn.kingyea.utils.LoginUtil;

public class HbaseTest {
	private final String ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME = "Client";
	private final String ZOOKEEPER_SERVER_PRINCIPAL_KEY = "zookeeper.server.principal";
	private final String ZOOKEEPER_DEFAULT_SERVER_PRINCIPAL = "zookeeper/hadoop";

	private Configuration conf = null;
	private Connection conn = null;
	private TableName tableName = TableName.valueOf("hshe_test1");
	private String family = "info";
	
	private String krb5File = null;
	private String userName = null;
	private String userKeytabFile = null;
	
	
	@Test
	public void testGetTableDesc(){
		System.out.println("开始执行 testGetTableDesc()！");
		Admin admin = null;
		try {
			admin = conn.getAdmin();
			
			if(admin.tableExists(tableName)){
				HTableDescriptor tableDesc = admin.getTableDescriptor(tableName);
				System.out.println(tableDesc);
				HColumnDescriptor[] columnFamilies = tableDesc.getColumnFamilies();
				for(int i=0;i<columnFamilies.length;i++){
					System.out.println(columnFamilies[i].getNameAsString());
				}
				
				
			}else{
				System.out.println("HTable:["+tableName.getNameAsString()+"]不存在！");
			}
			
		} catch (IOException e) {
			System.out.println("删除HTable:["+tableName.getNameAsString()+"]失败");
			e.printStackTrace();
		}finally {
			if(admin!=null){
				try {
					admin.close();
				} catch (IOException e) {
					System.out.println("关闭admin失败！");
					e.printStackTrace();
				}
			}
		}
		System.out.println("执行 testGetTableDesc() 完成！");
	}
	
	
	@Before
	public void init(){
		try {
			System.setProperty("hadoop.home.dir", "E:\\SOFTWARE\\大数据");
			conf = new Configuration();
			String userdir = System.getProperty("user.dir") + File.separator;
//			userdir +=  "src" + File.separator + "test" + File.separator + "resources" + File.separator + "hadoop" + File.separator;
			userdir += "conf" + File.separator;
			System.out.println(userdir);
			conf.addResource(new Path(userdir + "core-site.xml"));
			conf.addResource(new Path(userdir + "hdfs-site.xml"));
			conf.addResource(new Path(userdir + "hbase-site.xml"));
			
			if (User.isHBaseSecurityEnabled(conf)) {
				userName = "dev_root";
				userKeytabFile = userdir + "user.keytab";
				krb5File = userdir + "krb5.conf";
				
				LoginUtil.setJaasConf(ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME, userName, userKeytabFile);
				LoginUtil.setZookeeperServerPrincipal(ZOOKEEPER_SERVER_PRINCIPAL_KEY, ZOOKEEPER_DEFAULT_SERVER_PRINCIPAL);
				LoginUtil.login(userName, userKeytabFile, krb5File, conf);
			}
			conn = ConnectionFactory.createConnection(conf);
			
		} catch (IOException e) {
			System.out.println("登录失败！");
			e.printStackTrace();
		}
		
	}
	@After
	public void close(){
		try {
			conn.close();
		} catch (IOException e) {
			System.out.println("关闭连接失败！");
			e.printStackTrace();
		}
	}
}
